package gold.digger;

import gold.utils.InputUtil;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC1846 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int maximumElementAfterDecrementingAndRearranging(int[] arr) {
            int n = arr.length;
            int[] cnt = new int[n + 1];
            for (int num : arr) {
                cnt[Math.min(n, num)]++;
            }

            int miss = 0;
            for (int i = 1; i < n + 1; i++) {
                if (cnt[i] == 0) ++miss;
                else {
                    miss -= Math.min(miss, cnt[i] - 1);
                }
            }
            return n - miss;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[100,1,10000]");
        System.out.println(solution.maximumElementAfterDecrementingAndRearranging(arr));
    }

    public static void main(String[] args) throws Exception {
        LC1846 an = new LC1846();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
